
function add_int8(cmd_buf,num){
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(1);
		var dataview=new DataView(cmd_buf);
		dataview.setInt8(0,num);
	}
	else{
		var buf=new ArrayBuffer(1);
		var dataview=new DataView(buf);
		dataview.setInt8(0,num);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}
	
	return cmd_buf;
}

function get_int8(cmd_buf){
	var dataview=new DataView(cmd_buf);
	var num=dataview.getInt8(0);
	return [num,cmd_buf.slice(1)];
}

function add_int16(cmd_buf,num){
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(2);
		var dataview=new DataView(cmd_buf);
		dataview.setInt16(0,num);
	}
	else{
		var buf=new ArrayBuffer(2);
		var dataview=new DataView(buf);
		dataview.setInt16(0,num);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}
	
	return cmd_buf;
}

function get_int16(cmd_buf){
	var dataview=new DataView(cmd_buf);
	var num=dataview.getInt16(0);
	return [num,cmd_buf.slice(2)];
}

function add_int32(cmd_buf,num){
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(4);
		var dataview=new DataView(cmd_buf);
		dataview.setInt32(0,num);
	}
	else{
		var buf=new ArrayBuffer(4);
		var dataview=new DataView(buf);
		dataview.setInt32(0,num);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}
	
	return cmd_buf;
}

function get_int32(cmd_buf){
	var dataview=new DataView(cmd_buf);
	var num=dataview.getInt32(0);
	return [num,cmd_buf.slice(4)];
}

function add_uint32(cmd_buf,num){
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(4);
		var dataview=new DataView(cmd_buf);
		dataview.setUInt32(0,num);
	}
	else{
		var buf=new ArrayBuffer(4);
		var dataview=new DataView(buf);
		dataview.setUInt32(0,num);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}
	
	return cmd_buf;
}

function get_uint32(cmd_buf){
	var dataview=new DataView(cmd_buf);
	var num=dataview.getUInt32(0);
	return [num,cmd_buf.slice(4)];
}


function add_float(cmd_buf,num){
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(4);
		var dataview=new DataView(cmd_buf);
		dataview.setFloat32(0,num);
	}
	else{
		var buf=new ArrayBuffer(4);
		var dataview=new DataView(buf);
		dataview.setFloat32(0,num);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}
	
	return cmd_buf;
}

function get_float(cmd_buf){
	var dataview=new DataView(cmd_buf);
	var num=dataview.getFloat32(0);
	return [num,cmd_buf.slice(4)];
}


function add_str(cmd_buf,str){
	var len=str.utf8_byte_len();
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(len);
		var dataview=new DataView(cmd_buf);
		dataview.write_utf8(0,str);
	}
	else{
		var buf=new ArrayBuffer(len);
		var dataview=new DataView(buf);
		dataview.write_utf8(0,str);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}

	return cmd_buf;
}

function add_str16(cmd_buf,str){
	var len=str.utf8_byte_len();
	if (cmd_buf==null){
		cmd_buf=new ArrayBuffer(len+2);
		var dataview=new DataView(cmd_buf);
		dataview.setInt16(0,len);
		dataview.write_utf8(2,str);
	}else{
		var buf=new ArrayBuffer(len+2);
		var dataview=new DataView(buf);
		dataview.setInt16(0,len);
		dataview.write_utf8(2,str);
		cmd_buf=ArrayBuffer.concat([cmd_buf,buf]);
	}

	return cmd_buf;
}

function get_str(cmd_buf,len){
	var dataview=new DataView(cmd_buf);
	var str=dataview.read_utf8(0,len);
	return [str,cmd_buf.slice(len)];
}

function get_str16(cmd_buf){
	var dataview=new DataView(cmd_buf);
	var len=dataview.getInt16(0);
	var str=dataview.read_utf8(2,len);
	return [str,cmd_buf.slice(2+len)];
}

var protocol_tool={
	add_int8:add_int8,
	get_int8:get_int8,
	add_int16:add_int16,
	get_int16:get_int16,
	add_int32:add_int32,
	get_int32:get_int32,
	add_uint32:add_uint32,
	get_uint32:get_uint32,
	add_float:add_float,
	get_float:get_float,
	add_str:add_str,
	add_str16:add_str16,
	get_str:get_str,
	get_str16:get_str16,
};

module.exports=protocol_tool;